package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.join.account.group.request.SendJoinAccountGroupRequestDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.JoinAccountGroupRequestService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/join-account-group-request")
public class JoinAccountGroupRequestController {
    private final JoinAccountGroupRequestService joinAccountGroupRequestService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/count-received")
    public ResponseEntity<ApiResponse<Map<String, Integer>>> countReceived() {
        String userId = requestIdentityExtractor.getClaims().id;
        int result = joinAccountGroupRequestService.countReceived(userId);

        return httpResponseHelper.getSuccessResponse(Map.of("count", result));
    }

    @GetMapping("/query-receiving")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryReceiving(@RequestParam int pageSize, @RequestParam int pageIndex) {
        String userId = requestIdentityExtractor.getClaims().id;
        List<Map<String, Object>> result = joinAccountGroupRequestService.queryReceiving(userId, pageSize, pageIndex);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/send")
    public ResponseEntity<ApiResponse<Object>> send(@Valid @RequestBody SendJoinAccountGroupRequestDto data) {
        String receiverId = data.getReceiverId();
        List<String> roles = data.getRoles();
        MyClaims claims = requestIdentityExtractor.getClaims();
        String senderId = claims.id;
        String accountGroupId = claims.accountGroupId;
        joinAccountGroupRequestService.addOrUpdate(accountGroupId, senderId, receiverId, roles);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/accept")
    public ResponseEntity<ApiResponse<Object>> accept(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String receiverId = requestIdentityExtractor.getClaims().id;
        joinAccountGroupRequestService.accept(id, receiverId);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/reject")
    public ResponseEntity<ApiResponse<Object>> reject(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String receiverId = requestIdentityExtractor.getClaims().id;
        joinAccountGroupRequestService.reject(id, receiverId);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/delete")
    public ResponseEntity<ApiResponse<Object>> delete(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String receiverId = requestIdentityExtractor.getClaims().id;
        joinAccountGroupRequestService.reject(id, receiverId);

        return httpResponseHelper.getSuccessResponse();
    }
}
